const path = require('path')
/**
 * 获取vue组件列表
 * @param {*} importCodeList
 * @return {*}
 */
function getImportStr(importCodeList) {
  let str = `
${importCodeList['importList'].join('\n')}
// 获取要导入组件列表
export function getCompList(){
return [${importCodeList['compInfoList'].join(',')}]\n}`
  return str
}

/**
 * 导入用户配置的组件
 * @param {*} importCodeList
 * @param {*} compItemList vue组件列表
 * @param {*} stylePathList 样式路径列表
 */
function adduserImportList(importCodeList, compItemList, stylePathList) {
  for (const compItem of compItemList) {
    //  { isInstall: true, pathList: [Array] } ]
    let pathList = compItem['pathList']
    let isInstall = compItem['isInstall']
    let isRequire = compItem['isRequire']
    for (let filePath of pathList) {
      filePath = filePath.replace(/\\/g, '/')
      let fileObj = path.parse(filePath)
      let compName = fileObj['name'].toLocaleLowerCase()

      compName = compName.replace(/\-/g, '')

      // 是否是使用use导入
      let isInstallStr = `isInstall:${isInstall},`

      // 记录组件信息
      let importCode = `import ${compName} from '${filePath}'`
      if (isRequire) {
        // const elementPlus = require('element-plus')
        importCode = `const ${compName} = require('${filePath}')`
      }
      importCodeList['importList'].push(importCode)
      importCodeList['compInfoList'].push(`{ name: '${compName}',${isInstallStr} item: ${compName} }`)
      importCodeList['compPathList'].push(filePath)
    }

    // 导入样式
    for (const stylePath of stylePathList) {
      let importCode = `import '${stylePath}'`
      importCodeList['importList'].push(importCode)
    }
  }
  return importCodeList
}

module.exports = {
  getImportStr,
  adduserImportList
}
